package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbPerson;
import com.dongdongshop.service.PersonService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("person")
public class PersonController {

    @Reference
    private PersonService personService;

    @RequestMapping("getLoginInfo")
    @ResponseBody
    private Result getLoginInfo(TbPerson tbPerson){

        // 获取subject 对象
        Subject subject = SecurityUtils.getSubject();
        // 将账号密码封装
        UsernamePasswordToken token = new UsernamePasswordToken(tbPerson.getPname(),tbPerson.getPwd());
        // 调用subject 的login 方法执行登录

        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            System.out.println("账号不存在");
            return Result.build(ConstantEnum.REGISTER_ERROR);
        }catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
            return Result.build(ConstantEnum.PWD_ERROR);
        }

        return Result.build(ConstantEnum.LOGIN_SUCCESS);
    }

}
